﻿using System;
using System.Net;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject.Workflow;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using Sharp.WebBooter.Areas.Workflow.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;
namespace Sharp.WebBooter.Areas.Workflow.Controllers
{
    /// <summary>
    /// 流程定义控制器
    /// </summary>
    public class ProcessController : Controller
    {
        [AutomaticTemplate]
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                Model = new ProcessIndexModel(),
                 ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
            {
                var pageInfo = proxy.Channel.GetProcessPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }
        public ActionResult List()
        {
            return View();
        }
        [AutomaticTemplate]
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProcessDTO roleDto;
            using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
            {
                roleDto = proxy.Channel.GetProcessInfo(id.Value);
            }
            if (roleDto == null)
            {
                return HttpNotFound();
            }

            return new PartialViewResult { Model = roleDto.To<ProcessModel>() };
        }

        [AutomaticTemplate]
        public ActionResult Create()
        {
            return new PartialViewResult { Model = new ProcessModel() };
        }

        [AutomaticTemplate]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProcessDTO roleDto;
            using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
            {
                roleDto = proxy.Channel.GetProcessInfo(id.Value);
            }
            if (roleDto == null)
            {
                return HttpNotFound();
            }
            return new PartialViewResult { Model = roleDto.To<ProcessModel>() };
        }

        public ActionResult Edit2( )
        {
            return View("Edit");
        }
        public ActionResult Save(ProcessModel vm)
        {
            var dto = vm.To<ProcessDTO>();
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
            {
                res = proxy.Channel.SaveProcess(dto);
            }

            return this.JsonNet(res);
        }

        public ActionResult Delete(int[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
            {
                res = proxy.Channel.DeleteProcessById(ids);
            }

            return this.JsonNet(res);
        }

        public ActionResult Import()
        {
            return View();
        }
        public ActionResult DesignUI(string processGuid)
        {
            using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
            {
                var dto = proxy.Channel.GetProcess(processGuid);

                return View("DesignUI", dto);
            }
        }
    }
}